package com.yanqu.road.server.manger.activity.zhounianturntable;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.zhounianturntable.ZhouNianTurntableModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZhouNianTurntableMgr extends TempMgr {

    private static final Object lockObj = new Object();

    /**
     * 活动配置
     */
    public static Map<Integer, ZhouNianTurntableActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (ZhouNianTurntableActivity zhouNianTurntableActivity : activityMap.values()) {
            zhouNianTurntableActivity.save();
        }
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload ZhouNianTurntable activity start");
        Map<Integer, ZhouNianTurntableActivity> tempMap = new ConcurrentHashMap<>();
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ZhouNianTurntable.getValue());
        if (openActivityInfoList.size() <= 0) {
            return;
        }
        // 获取所有开启的活动的宝盘配置
        openActivityInfoList.forEach(activityInfo -> {
            synchronized (lockObj) {
                if (!tempMap.containsKey(activityInfo.getActivityId())) {
                    ZhouNianTurntableActivity zhouNianTurntableActivity = new ZhouNianTurntableActivity(activityInfo);
                    if (zhouNianTurntableActivity.init()) {
                        tempMap.put(activityInfo.getActivityId(), zhouNianTurntableActivity);
                    } else {
                        getLogger().error("load ZhouNian Turntable activity fail, activityId:{}", activityInfo.getActivityId());
                    }
                } else {
                    if (!tempMap.get(activityInfo.getActivityId()).reloadActivity(activityInfo)) {
                        getLogger().error("load ZhouNian Turntable activity fail, activityId:{}", activityInfo.getActivityId());
                    }
                }
            }
        });
        activityMap = tempMap;
        if (activityMap.size() > 0) {
            // 在线玩家操作
            for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
                player.getModule(ZhouNianTurntableModule.class).initSystem();
            }
        }
        getLogger().info("reload ZhouNianTurntable activity end");
    }

    public static ActivityInfo getInTimeActivityInfo() {
        return activityMap.values().stream()
                .map(ZhouNianTurntableActivity::getActivityInfo)
                .filter(ActivityMgr::activityInTime)
                .findFirst()
                .orElse(null);
    }

    public static Map<Integer, ZhouNianTurntableActivity> getActivityMap() {
        // 判断活动是不是有效,活动过期了将活动移出内存
        Map<Integer, ZhouNianTurntableActivity> temp = new ConcurrentHashMap<>(activityMap);
        temp.forEach((key, value) -> {
            if (!ActivityMgr.activityInShowTime(value.getActivityInfo())) {
                temp.remove(key);
            }
        });
        if (temp.size() != activityMap.size()) {
            activityMap = temp;
        }
        return activityMap;
    }

}
